package com.kehutong.auth.controller;

import com.kehutong.auth.entity.UserSearch;
import com.kehutong.common.util.Token;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.util.Objects;

@MvcAction
@ReqMapping("/admin/user/search")
public class UserSearchController {

	@Inject
    private JdbcSession jdbcSession;

	@ReqMapping("/getSearchInfo")
	public Object getSearchInfo(Token token) {
		if(Objects.isEmpty(token.getCompanyNo())){
			return Result.success();
		}

		UserSearch userSearch = Pool.get(UserSearch.class, token.getUuid());
		return Result.success(userSearch);
	}

	@ReqMapping("/editSearchInfo")
	public Object editSearchInfo(Token token, JSONObject jsonObject) {
		if(Objects.isEmpty(token.getCompanyNo())){
			return Result.success();
		}

		UserSearch userSearch = Pool.get(UserSearch.class, token.getUuid());
		if(Objects.nonNull(userSearch)){
			userSearch.setSearch(jsonObject.getString("search").trim());
			jdbcSession.updateById(userSearch);
		}

		return Result.success();
	}
	
}
